<?php
class rolesActions extends sfGraduaActions
{

    public function executeIndex($request)
    {
        $this->roles = RolesService::getAll();
        $this->permissions = RolesService::getAllPermissions();
        sfLoader::loadHelpers(array('Partial','Php2Js'));
        $this->roleViewTpl  = partial2Js('roles/roleView.jstpl',array(),true);
        $this->roleFormTpl  = partial2Js('roles/roleForm.jstpl',array(),true);
        $this->editDecorator   = partial2Js('globalPartials/editDecorator.jstpl',array(),true);
        $this->insertDecorator = partial2Js('globalPartials/insertDecorator.jstpl',array(),true);
        $this->viewDecorator   = partial2Js('globalPartials/viewDecoratorOver.jstpl',array(),true);
        $this->listTpl         = partial2Js('globalPartials/listTpl.jstpl',array(),true);
        $this->divListItemDecorator   = partial2Js('globalPartials/divListItemDecorator.jstpl',array(),true);
        $this->setLayout('layoutTasks');
    }

    function executeInsert($request) {
        $data = $this->getObjRequest('role',array('general'));
        $jsObjName = $this->getRequestParameter('jsObjName');
        $this->getResponse()->setTransferErrorFieldNames(array(
          'name','description'
        ));
        $this->getResponse()->setFieldIdPrefix('role');
        $id = RolesService::insert($data);
        $this->getResponseJsCall()
            ->addCommand('roles.list', 'refresh')
            ->addPHPParam('itemsDs', RolesService::getAll() )
            ->closeDialog()
            ->showFeedback('Inserido com sucesso!');
        return sfView::NONE;
    }
    
    function executeEdit($request) {
        $data = $this->getObjRequest('role',array('general'));
        $jsObjName = $this->getRequestParameter('jsObjName');
        $this->getResponse()->setTransferErrorFieldNames(array(
          'name','description','general'
        ));
        $data->permissions = isset($data->permissions) ? $data->permissions : array();
        RolesService::edit($data);
        $this->getResponseJsCall()
            ->addCommand($jsObjName, 'update')
            ->addPHPParam('ds', RolesService::get($data->id))
            ->showFeedback('Sucesso na edição.');
        return sfView::NONE;
    }

    function executeRemove($request) {
        //Dbg::x($this->getRequest()->getParameterHolder());
        $id = $this->getRequestParameter('id');
        $jsObjName = $this->getRequestParameter('jsObjName');
        RolesService::delete($id);
        $this->getResponseJsCall()
            ->addCommand($jsObjName,'remove')
            ->showFeedback('Usuário removido com sucesso!');
        return sfView::NONE;
    }
    
}
